package servlet.MaterialServlet;

import dao.MaterialDao;
import dao.PersonDAO;
import entity.MaterialBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import myUtil.personCheck;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/MaterialEditServlet")
public class MaterialEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        MaterialDao materialDao = new MaterialDao();
        String I = request.getParameter("id");
        MaterialBean obj = new MaterialBean();
        int curpage=0; //= (int)request.getAttribute("curpage");
        int pageCount=0; //= (int)request.getAttribute("pageCount");

        if(request.getParameter("curpage")!=null)
            curpage=Integer.parseInt(request.getParameter("curpage"));
        if(request.getParameter("pageCount")!=null)
            pageCount=Integer.parseInt(request.getParameter("pageCount"));
        System.out.println("materialEditServlet pageCount="+pageCount);
        System.out.println("materialEditServlet curpage="+curpage);
        obj.setItemId("");
        obj.setItemName("");
        obj.setItemClasses("");
        obj.setItemDescription("");
        obj.setVendorName("");
        obj.setContact("");
        obj.setSite("");
        obj.setInboundTime("");
        obj.setPhoneNumber("");
        if (I == null || I.equals("")) {
            // materialDao.addMaterial(obj.getItemId(),obj.getItemName(),obj.getItemClasses(),obj.getVendorName(),obj.getSite(),obj.getInboundTime(),obj.getContact(),obj.getPhoneNumber(),obj.getItemDescription());
            obj.setId(0);
        } else {
            try {
                int i = Integer.parseInt(I);
                obj = materialDao.findMaterialById(i);
                obj.setId(i);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        request.setAttribute("curpage",curpage);
        request.setAttribute("pageCount",pageCount);
        request.setAttribute("obj", obj);
        request.getRequestDispatcher("material_edit.jsp").forward(request, response);
    }
}
